package com.wenhui.schoolyardinfo.square.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.customview.GlideImageLoader;
import com.wenhui.schoolyardinfo.manager.DateManager;
import com.wenhui.schoolyardinfo.square.bean.PublicGoodBean;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/4 10:00
 * introduce : 公益Adapter
 */

public class PublicGoodAdapter extends RecyclerView.Adapter {

    private static final String TAG = "PublicGoodAdapter";
    private Context context;
    private List<String> pictureList = new ArrayList<>();
    private List<PublicGoodBean> publicGoodBeanList = new ArrayList<>();
    private static final int HEADER_FIRST_VIEW_TYPE = 1;
    private static final int HEADER_SECOND_VIEW_TYPE = 2;
    private static final int CONTENT_VIEW_TYPE = 3;

    public PublicGoodAdapter(Context context) {
        this.context = context;
    }

    public PublicGoodAdapter(Context context, List<PublicGoodBean> publicGoodBeanList) {
        this.context = context;
        this.publicGoodBeanList = publicGoodBeanList;
    }

    public PublicGoodAdapter(Context context, List<String> pictureList, List<PublicGoodBean> publicGoodBeanList) {
        this.context = context;
        this.pictureList = pictureList;
        this.publicGoodBeanList = publicGoodBeanList;
    }

    public void clearData() {
        pictureList.clear();
        publicGoodBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<String> pictureList, List<PublicGoodBean> publicGoodBeanList) {
        this.pictureList.addAll(pictureList);
        this.publicGoodBeanList.addAll(publicGoodBeanList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case HEADER_FIRST_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_public_good_header_first, parent, false);
                return new HeaderFirstHolder(view);
            case HEADER_SECOND_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_public_good_header_second, parent, false);
                return new HeaderSecondHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_public_good_content, parent, false);
                return new ContentHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderFirstHolder) {
            HeaderFirstHolder headerFirstHolder = (HeaderFirstHolder) holder;
            //设置banner样式
            headerFirstHolder.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            //设置图片加载器
            headerFirstHolder.banner.setImageLoader(new GlideImageLoader());
            //设置图片集合
            headerFirstHolder.banner.setImages(pictureList);
            //设置banner动画效果
            headerFirstHolder.banner.setBannerAnimation(Transformer.DepthPage);
            //设置标题集合（当banner样式有显示title时）
//            headerFirstHolder.banner.setBannerTitles(titles);
            //设置自动轮播，默认为true
            headerFirstHolder.banner.isAutoPlay(true);
            //设置轮播时间
            headerFirstHolder.banner.setDelayTime(3000);
            //设置指示器位置（当banner模式中有指示器时）
            headerFirstHolder.banner.setIndicatorGravity(BannerConfig.CENTER);
            //banner设置方法全部调用完毕时最后调用
            headerFirstHolder.banner.start();

        } else if (holder instanceof HeaderSecondHolder) {
            HeaderSecondHolder headerSecondHolder = (HeaderSecondHolder) holder;

        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            //加载宣传图片
            Glide.with(context).load(publicGoodBeanList.get(position - 2).getPublicGoodUrl()).into(contentHolder.imvPublicityPicture);
            contentHolder.tevTitle.setText(publicGoodBeanList.get(position - 2).getTitle());
            contentHolder.tevInitiator.setText(publicGoodBeanList.get(position - 2).getInitiator());

            String startTime[] = DateManager.millisecondConvertedToDate(publicGoodBeanList.get(position - 2).getStartTime()).split("-");
            String endTime[] = DateManager.millisecondConvertedToDate(publicGoodBeanList.get(position - 2).getEndTime()).split("-");
            contentHolder.tevTime.setText("时间：" +
                    startTime[0] + "." + startTime[1] + "." + startTime[2] + "-"
                    + endTime[0] + "." + endTime[1] + "." + endTime[2]);
            ;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER_FIRST_VIEW_TYPE;
        } else if (position == 1) {
            return HEADER_SECOND_VIEW_TYPE;
        } else {
            return CONTENT_VIEW_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return publicGoodBeanList.size() + 2;
    }


    protected class HeaderFirstHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.banner)
        Banner banner;

        private HeaderFirstHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            banner.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.banner:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    protected class HeaderSecondHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_region)
        TextView tevRegion;
        @BindView(R.id.layout_region)
        FrameLayout layoutRegion;
        @BindView(R.id.tev_time)
        TextView tevTime;
        @BindView(R.id.layout_time)
        FrameLayout layoutTime;
        @BindView(R.id.tev_state)
        TextView tevState;
        @BindView(R.id.layout_state)
        FrameLayout layoutState;

        private HeaderSecondHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutRegion.setOnClickListener(this);
            layoutTime.setOnClickListener(this);
            layoutState.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_region:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_time:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_state:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_publicity_picture)
        ImageView imvPublicityPicture;
        @BindView(R.id.tev_title)
        TextView tevTitle;
        @BindView(R.id.tev_initiator)
        TextView tevInitiator;
        @BindView(R.id.tev_time)
        TextView tevTime;
        @BindView(R.id.imv_partake)
        ImageView imvPartake;
        @BindView(R.id.tev_partake)
        TextView tevPartake;
        @BindView(R.id.layout_public_good)
        LinearLayout layoutPublicGood;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutPublicGood.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_public_good:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }
}
